package codepulse.blogmoon.cn.system.service.impl;

import codepulse.blogmoon.cn.system.dto.admin.RoleMenuRelDTO;
import codepulse.blogmoon.cn.system.entity.SysAdminAcctRoleRel;
import codepulse.blogmoon.cn.system.entity.SysAdminRoleMenuRel;
import codepulse.blogmoon.cn.system.service.SysAdminAcctRoleRelService;
import codepulse.blogmoon.cn.system.service.SysAdminRoleMenuRelService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import codepulse.blogmoon.cn.system.entity.SysAdminRole;
import codepulse.blogmoon.cn.system.service.SysAdminRoleService;
import codepulse.blogmoon.cn.system.mapper.SysAdminRoleMapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 61748
 * @description 针对表【sys_admin_role】的数据库操作Service实现
 * @createDate 2024-01-15 14:27:05
 */
@Service
public class SysAdminRoleServiceImpl extends ServiceImpl<SysAdminRoleMapper, SysAdminRole>
        implements SysAdminRoleService {

    @Resource
    private SysAdminAcctRoleRelService sysAdminAcctRoleRelService;

    @Resource
    private SysAdminRoleMenuRelService sysAdminRoleMenuRelService;

    @Override
    public List<SysAdminRole> getRoleByAcctId(Long bizAcctId) {
        return baseMapper.selectRoleByAcctId(bizAcctId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRole(Long id) {
        removeById(id);
        sysAdminAcctRoleRelService.remove(Wrappers.<SysAdminAcctRoleRel>lambdaQuery()
                .eq(SysAdminAcctRoleRel::getRoleId, id));
        sysAdminRoleMenuRelService.remove(Wrappers.<SysAdminRoleMenuRel>lambdaQuery()
                .eq(SysAdminRoleMenuRel::getRoleId, id));
    }

    @Override
    public List<Long> getMenuIdByRoleId(Long roleId) {
        List<SysAdminRoleMenuRel> menuRelList = sysAdminRoleMenuRelService.list(Wrappers.<SysAdminRoleMenuRel>lambdaQuery()
                .eq(SysAdminRoleMenuRel::getRoleId, roleId));
        if (CollectionUtils.isEmpty(menuRelList)) {
            return Lists.newArrayList();
        }
        return menuRelList.stream().map(SysAdminRoleMenuRel::getMenuId).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void saveMenuRel(RoleMenuRelDTO dto) {
        if (CollectionUtils.isEmpty(dto.getMenuIds())) {
            return;
        }
        sysAdminAcctRoleRelService.remove(Wrappers.<SysAdminAcctRoleRel>lambdaQuery()
                .eq(SysAdminAcctRoleRel::getRoleId, dto.getRoleId()));

        List<SysAdminRoleMenuRel> relList = dto.getMenuIds().stream().map(menuId -> {
            SysAdminRoleMenuRel roleMenuRel = new SysAdminRoleMenuRel();
            roleMenuRel.setRoleId(dto.getRoleId());
            roleMenuRel.setMenuId(menuId);
            return roleMenuRel;
        }).collect(Collectors.toList());
        sysAdminRoleMenuRelService.saveBatch(relList);
    }
}




